# encoding: UTF-8
import os

projectDir = '/home/adinlead/Project/JavaProject/cheriji'
fileTypes = ['.java', '.html', '.conf']
specificFile = ['routes']
ignoredDir = ['/.git', '/tmp', '/lib', '/.idea']


class FileTools:
    def __init__(self):
        pass

    def traverseFolder(self, dir=projectDir):
        filelist = []
        for dirpath, dirnames, filenames in os.walk(dir):
            ignored = False
            for igno in ignoredDir:
                if igno in dirpath:
                    ignored = True
            if not ignored:
                for name in filenames:
                    filepath = os.path.join(dirpath, name)
                    if self.file_extension(filepath) in fileTypes \
                            or name in specificFile:
                        filelist.append(filepath)

        return filelist

    def statisticLines(self, file):
        i = 0
        for line in open(file).readlines():
            if line != '\r\n':
                i = i + 1
        return i

    def file_extension(self, path):
        return os.path.splitext(path)[1]


if __name__ == '__main__':
    tools = FileTools()
    filelist = tools.traverseFolder()
    total = 0
    for file in filelist:
        filelines = tools.statisticLines(file)
        print file, '>>>>', filelines
        total += filelines
    print "工程有效代码总行数为：", total
